/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.sys.controller.ResourceController.java 
 * <p>
 * 类产生时间: 2014-5-12 下午 15:46
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.accident.controller;

import com.oceansoft.mobile.econsole.modules.accident.service.IAccidentService;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 事故管理控制器
 *
 * @author: xujin
 * @time: 2014-8-25 下午 18:23
 */
@Controller
@RequestMapping("accident")
public class AccidentController extends BaseController {

    @Resource
    private IAccidentService accidentService;

    /**
     * 进入事故主信息管理首页（列表）
     *
     * @return String
     */
    @RequestMapping(value = "index", method = GET)
    public String gotoAccidentIndex(Query query, Model model) {
//        query.setOrderField("id");
//        query.setOrderDirection("desc");
        model.addAttribute("query", query);
        model.addAttribute("pager", accidentService.findAccMainByCondition(query));
        log("查看事故列表");
        return "accident/accident-list";
    }

    /**
     * 列表分页显示
     *
     * @param query 分页查询条件
     * @param model 数据Model
     * @return 页面地址
     */
    @RequestMapping(value = "list", method = POST)
    public String loadPageByCondition(Query query, Model model) {
        Pager pager = accidentService.findAccMainByCondition(query);
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
        log(String.format("查看系统资源第%d页", query.getPageNum()));
        return "accident/accident-list";
    }

    /**
     * 查询事故明细
     *
     * @param accidentId 事故ID
     * @param model      页面数据MODEL
     * @return 页面路径
     */
    @RequestMapping(value = "detail/{accidentId}", method = GET)
    public String gotoAccidentDtl(@PathVariable("accidentId") String accidentId, Model model) throws Exception {
        model.addAllAttributes(accidentService.findAccidentInfo(accidentId));
        return "accident/accident-detail";
    }

    /**
     * 图片映射
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "photopath", method = GET)
    public void getPhoto(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String filePath = request.getParameter("path");
        File file = new File(configInfo.getFileUploadRootPath() + "/" + filePath);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
        byte[] b = new byte[1024];
        while (bis.read(b) > 0) {
            bos.write(b);
        }
        bis.close();
        bos.flush();
        bos.close();
    }

    /**
     * 事故责任文字转换
     *
     * @param liability 责任代号
     * @return String
     */
    private String trans2Str(int liability) {
        switch (liability) {
            case 1:
                return "无责";
            case 2:
                return "同责";
            case 3:
                return "全责";
        }
        return "";
    }
}
